
const express = require('express')
const router = express.Router()

// 获取KPI数据
router.get('/kpi', (req, res) => {
  const kpiData = {
    sortingDiff: 0.3,
    scanRate: 99.2,
    mape: 8.5,
    inventoryDiff: 0.7
  }
  res.json(kpiData)
})

// 获取风险预警
router.get('/alerts', (req, res) => {
  const alerts = [
    {
      id: 1,
      type: 'danger',
      timestamp: '2024-11-01 10:15:32',
      message: '区域A加工超时，已超过24小时限制'
    },
    {
      id: 2,
      type: 'warning',
      timestamp: '2024-11-01 09:45:12',
      message: '冷链车辆CV-001探头异常，温度偏差>1℃'
    },
    {
      id: 3,
      type: 'info',
      timestamp: '2024-11-01 08:30:45',
      message: '门店ST-015扫码率低于98%，已通知整改'
    }
  ]
  res.json(alerts)
})

// 获取温度监控数据
router.get('/temperature', (req, res) => {
  const temperatureData = {
    vehicles: [
      {
        name: '车辆1',
        data: generateTemperatureData()
      },
      {
        name: '车辆2',
        data: generateTemperatureData()
      },
      {
        name: '车辆3',
        data: generateTemperatureData()
      }
    ]
  }
  res.json(temperatureData)
})

const generateTemperatureData = () => {
  const data = []
  const now = new Date()
  for (let i = 0; i < 20; i++) {
    data.push([
      new Date(now.getTime() - (19 - i) * 60000).toISOString(),
      Math.random() * 4 - 2
    ])
  }
  return data
}

module.exports = router
